package com.api.gbhg.lbj.api;

import com.api.gbhg.lbj.dao.ExcelDao;
import com.api.gbhg.lbj.tool.ExcelTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.general.TimeUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.soa.workflow.request.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO
 * @date 2021/2/22 11:44
 */
public class BackupsApi {
    private final Logger logger= LoggerFactory.getLogger(BackupsApi.class);
    @POST
    @Path("/import")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getExcel(Map<String,String> param, @Context HttpServletRequest request, @Context HttpServletResponse
            response){
        logger.info("测试进入上传excle方法--");
        Integer fileId = Integer.parseInt(param.get("file"));
        User user = HrmUserVarify.getUser(request, response);//当前登录用户
        int uid = user.getUID();//获取用户id
        logger.info("fileId------"+fileId);
        InputStream is =  weaver.file.ImageFileManager.getInputStreamById(fileId);
        InputStream is2 =  weaver.file.ImageFileManager.getInputStreamById(fileId);
        InputStream is3 =  weaver.file.ImageFileManager.getInputStreamById(fileId);
        InputStream is4 =  weaver.file.ImageFileManager.getInputStreamById(fileId);
        InputStream is5 =  weaver.file.ImageFileManager.getInputStreamById(fileId);
        InputStream is6 =  weaver.file.ImageFileManager.getInputStreamById(fileId);
        logger.info("InputStream---is----"+is);
//        写
//        ImageFileManager imageFileManager = new ImageFileManager();
//        imageFileManager.setImagFileName(name);
//        imageFileManager.setData(data);
//        int fileId = imageFileManager.saveImageFile();
        RequestInfo requestinfo = new RequestInfo();
        requestinfo.setCreatorid("1");//创建者ID
        requestinfo.setWorkflowid("109"); // 工作流ID 92:通用项目表
        requestinfo.setRequestlevel("0"); // 紧急程度:0正常，1重要，2紧急
        requestinfo.setRemindtype("1"); // 提醒类型
        requestinfo.setIsNextFlow("0");//不自动流转至下一节点
        requestinfo.setDescription("lbj-excel测试"+ TimeUtil.getCurrentTimeString());//标题

        List<Object> sbdwlist = ExcelTools.findColumnRowId(is,"项目基本信息",null, "项目单位", 0);
        logger.info("sbdwlist---:"+sbdwlist);
        String sbdw = sbdwlist.get(1).toString();//获取项目单位
        logger.info("sbdw:"+sbdw);

        List<Object> ndlist = ExcelTools.findColumnRowId(is2,"项目基本信息",null, "计划开始执行年份", 0);
        logger.info("ndlist---:"+ndlist);
        String nd = ndlist.get(1).toString();//年度
        logger.info("nd:"+nd);

        List<Object> xmmclist = ExcelTools.findColumnRowId(is3,"项目基本信息",null,"项目名称", 0);
        logger.info("xmmclist---:"+xmmclist);
        String xmmc = xmmclist.get(1).toString();//获取项目名称
        logger.info("xmmc----:"+xmmc);


        List<Object> dhlist = ExcelTools.findColumnRowId(is4,"项目基本信息",null, "联系人电话", 0);
        logger.info("dhlist---:"+dhlist);
        String lxdh = dhlist.get(1).toString();//联系人电话
        logger.info("lxdh:"+lxdh);

        List<Object> fzrlist = ExcelTools.findColumnRowId(is5,"项目基本信息",null, "项目负责人", 0);
        logger.info("fzrlist---:"+fzrlist);
        String fzr = fzrlist.get(1).toString();//负责人
        logger.info("fzr:"+fzr);
//
//        logger.info("项目单位:"+sbdw+",负责人:"+fzr+",项目名称:"+xmmc+",年度:"+nd+"联系人电话:"+lxdh);
        MainTableInfo table = new MainTableInfo();
        table.addProperty(getPro("xmmc",xmmc+TimeUtil.getCurrentTimeString()));
        table.addProperty(getPro("sbdw", ExcelDao.getSBDWid(sbdw)));
        table.addProperty(getPro("sbks","169"));
        table.addProperty(getPro("sfdrgd","0"));//是否直接归档
        table.addProperty(getPro("ysnd",nd));
        table.addProperty(getPro("sfwdlhsdw","0"));
        table.addProperty(getPro("fzr",ExcelDao.getFZRid(fzr)));
        table.addProperty(getPro("jbr","1"));
        table.addProperty(getPro("lxdh",lxdh));
        table.addProperty(getPro("cjr",lxdh));//创建人

//        table.addProperty(getPro("xmmc","123"));
//        table.addProperty(getPro("sbdw", "47"));
//        table.addProperty(getPro("sbks","169"));
//        table.addProperty(getPro("sfdrgd","0"));//是否直接归档
//        table.addProperty(getPro("ysnd","2021"));
//        table.addProperty(getPro("sfwdlhsdw","0"));
//        table.addProperty(getPro("fzr","1"));
//        table.addProperty(getPro("jbr","1"));
//        table.addProperty(getPro("lxdh","100920101"));
        requestinfo.setMainTableInfo(table);

        DetailTableInfo tables = new DetailTableInfo();
        DetailTable OneTable = new DetailTable();
        //ExcelDao excelDao=new ExcelDao();
        // List<ExcelBizHelper> exceData = excelDao.getExceData(is6);
        OneTable.setId("4");//表示明细表4
        for(int i=0;i<1;i++){//明细表行数
            Row row = new Row();
//            String deptBudget = exceData.get(i).getDeptBudget();
//            deptBudget=deptBudget.substring(deptBudget.indexOf("[")+1,deptBudget.indexOf("]"));
//            String governmentBudget = exceData.get(i).getGovernmentBudget();
//            governmentBudget=governmentBudget.substring(governmentBudget.indexOf("[")+1,governmentBudget.indexOf("]"));
//            row.addCell(getCell("bmyszcjjfl",deptBudget));//部门预算支出经济分类
//            row.addCell(getCell("hjwy",exceData.get(i).getTotal()));//合计（万元）
//            row.addCell(getCell("dyzfjjfl",governmentBudget));//对应政府经济分类
            row.addCell(getCell("bmyszcjjfl","31003"));//部门预算支出经济分类
            row.addCell(getCell("hjwy","200.00"));//合计（万元）
            row.addCell(getCell("dyzfjjfl","50601"));//对应政府经济分类
            OneTable.addRow(row);
        }
        tables.addDetailTable(OneTable);
        requestinfo.setDetailTableInfo(tables);
        RequestService requestService  = new RequestService();
        String requestid = "";
        try {
            requestid = requestService.createRequest(requestinfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("ImportExcelApi流程创建成功="+requestid);
        return requestid;
    }
    public Property getPro(String field,String value) {
        Property property = new Property();
        property.setName(field);
        property.setValue(value);
        return property;
    }

    public Cell getCell(String field,String value) {
        Cell cel = new Cell();
        cel.setName(field);
        cel.setValue(value);
        return cel;
    }
}
